import collections


class Solution(object):
    def groupStrings(self, strings):
        """
        :type strings: List[str]
        :rtype: List[List[str]]
        """

        def base(s):
            diff = ord(s[0]) - ord('a')
            r = ''
            for item in s:
                r += chr((ord(item) - ord('a') - diff) % 26 + ord('a'))
            return r

        d = collections.defaultdict(list)
        for s in strings:
            d[base(s)].append(s)
        out = []
        for key in d:
            out.append(d[key])

        return out


data = Solution()
strings = ["abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"]
print(data.groupStrings(strings))
